"""
施工、监理通用
"""
import logging


from Config.config import *
import allure, os, re


from Config.AutoFlowEngine import autoflow
from Config.Base import Base
from Config.URL import *
from Config.Tool import tool
from playwright.sync_api import Page, expect, Playwright
class CBJL:
    '''
        初始化类
        调用CBJL类初始化记得传数据
    '''
    def __init__(self,playwright,case_data = None):
        self.case_data = case_data
        self.SetupTeardown = Base(playwright)

    def start(self):
        allure.dynamic.feature(self.case_data['module'])
        allure.dynamic.title(self.case_data['caseid'])
        allure.dynamic.suite(self.case_data['caseid'])

        self.SetupTeardown.goto(url)
        self.SetupTeardown.login(self.case_data['account1'], password_admin)
        page = self.SetupTeardown.page


        self.SetupTeardown.expect_login()
        allure.step("跳转CB")
        CBXXX = "/#/projectLevel/constructionSupervision/noticeToProceed/noticeToProceed/CB01?type=CB01"
        CBurl = CBXXX.replace("CB01", self.case_data['caseid'])
        page.goto(url + CBurl)
        self.SetupTeardown.select_bd(bd_zhihuigu)
        page.get_by_role("button", name=" 新建").click()
        page.wait_for_timeout(1000)
        allure.step("选择标段")
        if page.get_by_label("请选择标段").is_visible():
            page.get_by_label("请选择标段").get_by_text(self.case_data['lot']).click()
            page.get_by_role("button", name="确 定").click()
            page.wait_for_timeout(1000)
        allure.step("输入名称")
        name = tool.nowtime(YMD="ymd", Name=self.case_data['caseid'])
        if page.locator("form").filter(has_text="新建").get_by_placeholder("请输入标题").is_visible():
            page.locator("form").filter(has_text="新建").get_by_placeholder("请输入标题").fill(name)

        allure.step("输入所属年")
        if page.get_by_placeholder("请选择所属年").is_visible():
            page.get_by_placeholder("请选择所属年").click()
            page.get_by_placeholder("请选择所属年").fill(tool.nowtime("ymd"))
            page.get_by_placeholder("请选择所属年").press("Enter")
        allure.step("输入所属月")
        if page.get_by_placeholder("请选择所属月").is_visible():
            page.get_by_placeholder("请选择所属月").click()
            page.get_by_placeholder("请选择所属月").fill(tool.nowtime("ymd"))
            page.get_by_placeholder("请选择所属月").press("Enter")
        allure.step("业务表单")
        if page.get_by_placeholder("请选择业务表单").is_visible():
            page.get_by_placeholder("请选择业务表单").click()
            page.locator("li").filter(has_text="CB01-").click()
        allure.step("填写业务表单")
        if page.get_by_role("heading", name="业务表单").is_visible():
            trcount = page.get_by_text("业务表单新增表单表单填报").locator("//table/tbody/tr").count()
            print("业务表单填报：" + str(trcount))
            for tr in page.get_by_text("业务表单新增表单表单填报").locator("//table/tbody/tr").all():
                page.get_by_text("未填报").first.click()
                page.get_by_role("button", name=" 表单填报").click()
                page.wait_for_timeout(1000)
                if page.get_by_role("button", name="保存", exact=True).is_visible():
                    page.get_by_role("button", name="保存", exact=True).click()
                    expect(page.locator("body")).to_contain_text("填报成功")
                elif page.get_by_text("上传填报后文件").is_visible():
                    page.get_by_text("上传填报后文件").click()
                    # page.locator("div").filter(has_text=re.compile(r"^上传填报后文件$")).nth(2).click()

                    with page.expect_file_chooser() as fc_info:
                        page.get_by_text("上传填报后文件").click()
                    file_chooser = fc_info.value
                    file_chooser.set_files(test_datas_dir + "\填报.docx")
                    expect(page.locator("body")).to_contain_text("导入成功")

        allure.step("上传签章文件")
        if page.get_by_role("heading", name="附件信息").is_visible():
            page.get_by_role("button", name=" 新建").nth(1).click()
            page.get_by_label("附件").locator("div").filter(has_text=re.compile(r"^是否签章$")).locator("span").click()
            with page.expect_file_chooser() as fc_info:
                page.get_by_role("button", name="上传文件").click()
            file_chooser = fc_info.value
            file_chooser.set_files(test_datas_dir + "\签章.pdf")
            allure.step("等待上传完成")
            page.wait_for_timeout(2000)
            page.get_by_role("button", name="确定").click()

        allure.step("发起流程")
        tool.starflow(page, self.case_data['username1'])

        page = autoflow(self.SetupTeardown, name)

        page.wait_for_timeout(2000)
        page.goto(url + CBurl)
        self.SetupTeardown.select_bd(bd_zhihuigu)
        page.get_by_role("button", name=" 查询").click()
        page.wait_for_timeout(1000)
        #page.get_by_text(name).first.click()
        page.get_by_text("AT2024-04-24_CB02").first.click()

        with page.expect_popup() as page1_info:
            page.get_by_text("签章文件").first.click()
        page1 = page1_info.value
        # 测试结果截图
        page.wait_for_timeout(1000)
        filename = os.path.join(test_screenshot_dir, f"{'CB01签章文件1'}.png")
        page1.screenshot(path=filename)
        allure.attach.file(source=filename, name='签章文件1',
                           attachment_type=allure.attachment_type.PNG)

        with page.expect_popup() as page2_info:
            page.get_by_text("签章文件").nth(1).click()
        page2 = page2_info.value
        # 测试结果截图
        page.wait_for_timeout(1000)
        filename = os.path.join(test_screenshot_dir, f"{'CB01签章文件2'}.png")
        page2.screenshot(path=filename)
        allure.attach.file(source=filename, name='签章文件2',
                           attachment_type=allure.attachment_type.PNG)

        with page.expect_popup() as page3_info:
            page.get_by_text("签章文件").nth(2).click()
        page3 = page3_info.value
        # 测试结果截图
        page.wait_for_timeout(1000)
        filename = os.path.join(test_screenshot_dir, f"{'CB01签章文件3'}.png")
        page3.screenshot(path=filename)
        allure.attach.file(source=filename, name='签章文件3',
                           attachment_type=allure.attachment_type.PNG)

        self.SetupTeardown.close()


    #Base = Base(playwright)


